package com.pxxy.mall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pxxy.common.constant.ProductConstant;
import com.pxxy.common.constant.WareConstant;
import com.pxxy.mall.ware.entity.PurchaseDetailEntity;
import com.pxxy.mall.ware.service.PurchaseDetailService;
import com.pxxy.mall.ware.service.WareSkuService;
import com.pxxy.mall.ware.vo.MergeVo;
import com.pxxy.mall.ware.vo.PurchaseDoneVo;
import com.pxxy.mall.ware.vo.PurchaseItemDoneVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxxy.common.utils.PageUtils;
import com.pxxy.common.utils.Query;

import com.pxxy.mall.ware.dao.PurchaseDao;
import com.pxxy.mall.ware.entity.PurchaseEntity;
import com.pxxy.mall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
@AllArgsConstructor
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    private PurchaseDetailService purchaseDetailService;


    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils getUnreceiveQueryPage(Map<String, Object> params) {
        LambdaQueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<PurchaseEntity>().lambda()
                .eq(PurchaseEntity::getStatus, WareConstant.PurchaseDetailStatusEnum.CREATE.getCode())
                .or().eq(PurchaseEntity::getStatus, WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
        IPage<PurchaseEntity> page = this.page(new Query<PurchaseEntity>().getPage(params), wrapper

        );
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void merge(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();
        if (purchaseId == null) {
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATE.getCode());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = mergeVo.getItems();

        purchaseDetailService.update(new UpdateWrapper<PurchaseDetailEntity>()
                .lambda().set(PurchaseDetailEntity::getPurchaseId, purchaseId)
                .set(PurchaseDetailEntity::getStatus, WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode())
                .in(PurchaseDetailEntity::getId, items)
        );
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);


    }


    @Transactional
    @Override
    public void received(List<Long> purchaseId) {
        List<PurchaseEntity> collect = purchaseId.stream().map(id -> {
                    PurchaseEntity byId = this.getById(id);
                    return byId;
                }).filter(t -> t.getStatus() == WareConstant.PurchaseStatusEnum.CREATE.getCode()
                        || t.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode())
                .map(entity -> {
                    entity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                    entity.setUpdateTime(new Date());
                    return entity;
                }).collect(Collectors.toList());
        //修改采购单的状态
        this.updateBatchById(collect);
        //修改采购需求的状态
        List<Long> purchaseIds = collect.stream().map(PurchaseEntity::getId).collect(Collectors.toList());
        purchaseDetailService.update(new UpdateWrapper<PurchaseDetailEntity>().lambda()
                .set(PurchaseDetailEntity::getStatus, WareConstant.PurchaseDetailStatusEnum.BUYING.getCode())
                .in(PurchaseDetailEntity::getPurchaseId, purchaseIds));
    }

    /**
     * 完成采购
     */
    @Transactional
    @Override
    public void done(PurchaseDoneVo purchaseDoneVo) {
        PurchaseEntity byId = this.getById(purchaseDoneVo.getId());
        if (byId != null && byId.getStatus() == WareConstant.PurchaseStatusEnum.RECEIVE.getCode()) {
            boolean flag = true;
            for (PurchaseItemDoneVo item : purchaseDoneVo.getItems()) {
                if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                    flag = false;
                } else {
                    PurchaseDetailEntity entity = purchaseDetailService.getById(item.getItemId());
                    if (Objects.nonNull(entity) && entity.getStatus() == WareConstant.PurchaseDetailStatusEnum.BUYING.getCode()) {
                        //修改库存
                        wareSkuService.addStock(entity.getSkuId(), entity.getWareId(), entity.getSkuNum());
                        //修改采购需求的状态
                        List<PurchaseDetailEntity> purchaseDetailEntitys = purchaseDoneVo.getItems().stream().map(t -> {
                            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                            purchaseDetailEntity.setId(t.getItemId());
                            purchaseDetailEntity.setStatus(t.getStatus());
                            return purchaseDetailEntity;
                        }).collect(Collectors.toList());
                        purchaseDetailService.updateBatchById(purchaseDetailEntitys);
                    } else return;

                }
            }
            //修改采购单的状态和
            if (flag) {
                byId.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
            } else {
                byId.setStatus(WareConstant.PurchaseStatusEnum.HASERROR.getCode());
            }
            this.updateById(byId);

        }
    }


}